#!/usr/bin/env python
# -*- coding: UTF-8 -*-

#  protocol_combo.py
#
#  Copyright (c) 2006-2007 INdT (Instituto Nokia de Technologia)
#                2007 Kenneth Rohde Christiansen
#                2007 Marcelo Lira dos Santos
#
#  Author: Kenneth Rohde Christiansen <kenneth.christiansen@gmail.com>
#          Renato Araujo Oliveira Filho <renato.filho@indt.org.br>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import pygtk
pygtk.require("2.0")
import gtk
import pango
import os
import tapioca

import util
import icon_theme

class ProtocolCombo(gtk.ComboBox):
    (
    PROTOCOL_XMPP,
    PROTOCOL_XMPP_LINK_LOCAL,
    PROTOCOL_MSN,
    PROTOCOL_IRC,
    ) = range(4)

    (
    COMBO_ACCOUNT_IMAGE,
    COMBO_ACCOUNT_TEXT,
    COMBO_ACCOUNT_CONNECTION_MANAGER,
    COMBO_ACCOUNT_PROTOCOL,
    COMBO_ACCOUNT_FIXED
    ) = range(5)

    def __init__(self):
        gtk.ComboBox.__init__(self)
        self.clear()

        cell_pix = gtk.CellRendererPixbuf()
        cell_pix.set_fixed_size(22, -1)
        self.pack_start(cell_pix, False)
        self.add_attribute(cell_pix, "pixbuf", 0)

        cell_text = gtk.CellRendererText()
        cell_text.set_property('ellipsize', pango.ELLIPSIZE_END)
        self.pack_start(cell_text, True)
        self.add_attribute(cell_text, "markup", 1)

        self.protocol_store = gtk.ListStore (gtk.gdk.Pixbuf, # Account image
                            str,        # Account text
                            str,        # Connection Manager
                            int,        # Protocol identifier
                            bool        # Fixed account
        )

        self.set_model(self.protocol_store)

        self.protocol_icon = {
            ProtocolCombo.PROTOCOL_XMPP :
            icon_theme.icon_theme_get_default().\
                load_icon("protocol-google-talk", 16, 0),
            ProtocolCombo.PROTOCOL_MSN :
            icon_theme.icon_theme_get_default().\
                load_icon("protocol-msn", 16, 0),
            ProtocolCombo.PROTOCOL_XMPP_LINK_LOCAL:
            icon_theme.icon_theme_get_default().\
                load_icon("protocol-link-local", 16, 0),
        }

        print 'has_gabble: ',
        print util.has_connection_manager("gabble")

        if util.has_connection_manager("gabble"):
            pixbuf = self.protocol_icon[ProtocolCombo.PROTOCOL_XMPP]
            self.protocol_store.append([pixbuf, "Google Talk", "gabble",
                        ProtocolCombo.PROTOCOL_XMPP, True])

        if util.has_connection_manager("butterfly"):
            pixbuf = self.protocol_icon[ProtocolCombo.PROTOCOL_MSN]
            self.protocol_store.append([pixbuf, "MSN Messenger (Experimental)", \
                        "butterfly", ProtocolCombo.PROTOCOL_MSN, True])

        if util.has_connection_manager("salut"):
            pixbuf = self.protocol_icon[ProtocolCombo.PROTOCOL_XMPP_LINK_LOCAL]
            self.protocol_store.append([pixbuf, "Link-Local Messaging", "salut",
                        ProtocolCombo.PROTOCOL_XMPP_LINK_LOCAL,
                        True])


    def set_selected_protocol(self, protocol):
        iter = self.protocol_store.get_iter_first()
        while iter:
            _protocol = self.protocol_store.get_value(iter, \
                    ProtocolCombo.COMBO_ACCOUNT_PROTOCOL)
            if _protocol == protocol:
                self.set_active_iter(iter)
            iter = self.protocol_store.iter_next(iter)


    def get_protocol_icon(self, protocol):
        return self.protocol_icon[protocol]


    def get_selected_protocol(self):
        iter = self.get_active_iter()
        return self.protocol_store.get(iter, \
                ProtocolCombo.COMBO_ACCOUNT_PROTOCOL)[0]


    def get_selected_protocol_icon(self):
        iter = self.get_active_iter()
        return self.protocol_store.get(iter, \
                ProtocolCombo.COMBO_ACCOUNT_IMAGE)[0]

